import allure

from src.api.base_api import BaseApi
from src.utils.get_today_time import get_n_day_end_before_today, get_n_day_before_today


class Schedule(BaseApi):
    def __init__(self, base_url):
        super().__init__(base_url)

    @allure.step('添加排期')
    def add_schedule(self, platform, author_id, day, **kwargs):
        '''
        url=https://ck-medium-api-test.cds8.cn/v1/commerce/schedule
        post
        commission_rate:线上佣金率 100%
        offline_commission_rate:线下佣金率 100%
        estimate_order_volume:预估销量
        goods:[
                {
                    "product_id": "3680297058771796292",
                    "estimate_order_volume": 33698,
                    "commission_rate": None,
                    "offline_commission_rate": None
                }
            ]
        '''
        data = {
            "platform": platform,
            "author_id": author_id,
            "goods": None,
            "start_time": get_n_day_before_today(day),
            "end_time": get_n_day_end_before_today(),
            "estimate_order_amount": 666,
            "pit_local_fee": None,
            "investment_fee": None,
            "is_record": None,
            "remark": None,
            "remind": None,
            "tag_id": None,
            "files": [],
            "reason": None
        }
        for k, v in kwargs.items():
            data[k] = v

        res_json = self.send(path='commerce/schedule', method='post', json=data)

        return res_json

    @allure.step('删除排期')
    def delete_schedule(self, schedule_id, **kwargs):
        '''
        url=https://ck-medium-api-test.cds8.cn/v1/commerce/schedule/delete
        put
        '''

        json_data = {
            "schedule_id": schedule_id,
            "delete_reason": "xxxxxx"
        }
        for k, v in kwargs.items():
            json_data[k] = v
        res_json = self.send(method='put', path='commerce/schedule/delete', json=json_data)

        return res_json

    @allure.step('查看排期列表')
    def list(self, platform, day=30, **kwargs):
        '''
        url=https://ck-medium-api-test.cds8.cn/v1/commerce/schedule/list
        post
        '''
        json = {
            "product_ids": None,
            "live_tag_ids": None,
            "author_tag_ids": None,
            "author_tag_type": None,
            "product_class": None,
            "platform": platform,
            "dept_id": None,
            "user_ids": None,
            "start_time": get_n_day_before_today(day),
            "end_time": get_n_day_end_before_today(),
            "follower_count": None,
            "deliver_status": None,
            "author_id": None,
            "shop_ids": None,
            "page": 1,
            "size": 10,
            "sort": "",
            "order_by": 0
        }
        for k, v in kwargs.items():
            json[k] = v

        res_json = self.send(method='post', json=json, path='commerce/schedule/list')

        return res_json

    @allure.step('点击排期详细信息')
    def get_schedule_detail(self, schedule_id):
        '''
        url=https://ck-medium-api-test.cds8.cn/v1/commerce/schedule/detail
        get
        '''
        params = {
            'schedule_id': schedule_id
        }
        res_json = self.send(method='get', params=params, path='commerce/schedule/detail')
        return res_json

    @allure.step('更新排期')
    def update_schedule(self, **kwargs):
        data = {
            "schedule_id": 360,
            "platform": "dy",
            "author_id": "935008751591939",
            "goods": [
                {
                    "product_id": "3680297058771796292",
                    "estimate_order_volume": 33698,
                    "commission_rate": 0,
                    "offline_commission_rate": 0
                }
            ],
            "start_time": 1716566400,
            "end_time": 1716739199,
            "estimate_order_amount": 666,
            "pit_local_fee": 993344,
            "investment_fee": 993355,
            "is_record": 2,
            "remark": "9933365555ppp",
            "remind": 24,
            "tag_id": 6,
            "files": [],
            "reason": "了噼里啪啦"
        }
        for k, v in kwargs.items():
            data[k] = v

        res_json = self.send(path='commerce/schedule', method='put', json=data)

        return res_json

    @allure.step('手动交付，抖音平台除外')
    def manaual_schedule(self,**kwargs):
        '''
        url=https://ck-medium-api-test.cds8.cn/v1/commerce/schedule/complete/manual
        post
        '''
        json = {
            "schedule_id": 379,
            "schedule_live_id": 0,
            "live_id": "",
            "live_name": "阿萨德",
            "live_start_time": get_n_day_before_today(1),
            "goods": [
                {
                    "product_id": "20931194049964",
                    "order_volume": 123
                },
                {
                    "product_id": "20972854380964",
                    "order_volume": 123
                }
            ],
            "order_amount": "123",
            "refund_amount": "123",
            "remark": "123"
        }
        for k,v in kwargs.items():
            json[k]=v

        res_json = self.send(method='post',path='commerce/schedule/complete/manual',json=json)

        return res_json



